<?php
    require  "../init.php";

    if(empty($_SESSION['admin'])){
        admin_redirect("请登录",3,ADMIN_ROOT_URL . "login.php");
        die;
    }

    //分页查找跳转的地址
    $url = "./index.php";

    $name = '';
    $where = '';
    $where1 = '';
    $urlname = '';
    if(isset($_GET['name']) && !empty($_GET['name'])){
        $name = $_GET['name'];
        $where = " where gname like '%$name%'";
        $where1 = " and g.gname like '%$name%'";
        $urlname = "&name=$name";
    }

    //得到要显示的数据的总条数
    $sql = "select count(*) as total from " .PRE . "goods" .$where;
    $result = query($link, $sql);
    $total = $result[0]['total'];

    //每页显示的记录数
    //$num = PAGE_NUM;//
    $num = 1;

    //总页数 = ceil(总条数 / 总页数)
    $total_page = ceil($total / $num);

    //当前页码
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

    //限制页码的范围 (1,$total_page)
    // 如果页码0小于1,则$min=1;1 < $total_page(总页数),则$page=1
    // 如果页码10大于$total_page,则$min=10;10 > $total_page(总页数),则$page=$total_page
    $min = max(1, $page);
    $page = min($min, $total_page);

    //获取偏移量 (当前页码 - 1) * 每页的显示量
    $offset = ($page - 1) * $num;

    //上一页和下一页的页码
    $prev = $page - 1;
    $next = $page + 1;

    //制作数字页码
    $num_link = '';

    //制作数字页码的开始与结束
    $start = $page - 2;
    $end = $page + 2;

    //限制开始与结束页码
    $start = max(1, $start);
    $end = min($total_page, $end);

    for($i = $start; $i <= $end; $i++){
        if($page == $i){
            $num_link .= '<li class="active"><a href="./index.php?page='.$i.$urlname.'" >'.$i.'</a></li>';
            continue;
        }
        $num_link .= '<li><a href="./index.php?page='.$i.$urlname.'" >'.$i.'</a></li>';
    }

    //准备sql语句
    $sql = "select g.id, g.gname, g.cate_id, g.stock, g.sale, g.is_new, g.is_hot, g.state, g.zan, g.msg, g.create_time, c.cname,i
.iname,g.price,g.update_time
 from ".PRE . "goods as g," . PRE . "category as c," .PRE . "image as i " .
        "where c.id = g.cate_id and g.id = i.goods_id and i.cover = 1".
        $where1 ." limit  $offset,$num";

    //执行sql语句
    $result = query($link, $sql);

    //获取受影响行数,也就是每页的记录数
    $row = mysqli_affected_rows($link);



    //处理结果集
    mysqli_close($link);
?>

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
    <meta name="author" content="GeeksLabs">
    <meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
    <link rel="icon" href="../../public/admin/img/icons/logo.jpg">

    <title>小米 后台(商品列表)</title>

    <!-- Bootstrap CSS -->
    <link href="../../public/admin/css/bootstrap.min.css" rel="stylesheet">
    <!-- bootstrap theme -->
    <link href="../../public/admin/css/bootstrap-theme.css" rel="stylesheet">
    <!--external css-->
    <!-- font icon -->
    <link href="../../public/admin/css/elegant-icons-style.css" rel="stylesheet" />
    <link href="../../public/admin/css/font-awesome.min.css" rel="stylesheet" />
    <!-- full calendar css-->
    <link href="../../public/admin/assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet" />
    <link href="../../public/admin/assets/fullcalendar/fullcalendar/fullcalendar.css" rel="stylesheet" />
    <!-- easy pie chart-->
    <link href="../../public/admin/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
    <!-- owl carousel -->
    <link rel="stylesheet" href="../../public/admin/css/owl.carousel.css" type="text/css">
    <link href="../../public/admin/css/jquery-jvectormap-1.2.2.css" rel="stylesheet">
    <!-- Custom styles -->
    <link rel="stylesheet" href="../../public/admin/css/fullcalendar.css">
    <link href="../../public/admin/css/widgets.css" rel="stylesheet">
    <link href="../../public/admin/css/style.css" rel="stylesheet">
    <link href="../../public/admin/css/style-responsive.css" rel="stylesheet" />
    <link href="../../public/admin/css/xcharts.min.css" rel=" stylesheet">
    <link href="../../public/admin/css/jquery-ui-1.10.4.min.css" rel="stylesheet">
    <link href="../../public/admin/css/index.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
    <!--[if lt IE 9]>
    <script src="../../public/admin/js/html5shiv.js"></script>
    <script src="../../public/admin/js/respond.min.js"></script>
    <script src="../../public/admin/js/lte-ie7.js"></script>
    <![endif]-->
</head>

<body>
<!-- container section start -->
<section id="container" class="">
    <!--引入头部-->
    <?php require '../top.php'?>
    <!--引入左边菜单栏-->
    <?php require '../aside.php'?>

    <!--main content start-->
    <section id="main-content">
        <section class="wrapper">
            <!-- page start-->
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            商品列表
                            <form class="navbar-form navbar-right" role="search" action="">
                                <div class="form-group">
                                    <input type="text" class="form-control" name="name" placeholder="商品名">
                                </div>
                                <button type="submit" class="btn btn-default">搜索</button>
                            </form>
                        </header>
                        <?php foreach($result as $key => $value):?>
                            <table class="table table-striped table-advance table-hover">
                                <tbody>
                                    <tr class="table_center">
                                        <th><i class="icon_profile"></i> ID</th>
                                        <th><i class="icon_profile"></i> 名称</th>
                                        <th><i class="icon_mobile"></i> 分类</th>
                                        <th><i class="icon_mobile"></i> 价格</th>
                                        <th><i class="icon_mail_alt"></i> 库存</th>
                                        <th><i class="icon_pin_alt"></i>销量</th>
                                        <th><i class="icon_calendar"></i> 是否新品</th>
                                        <th><i class="icon_calendar"></i> 是否热销产品</th>
                                        <th><i class="icon_calendar"></i> 是否上架</th>
                                        <th><i class="icon_calendar"></i> 集赞数量</th>

                                    </tr>

                                        <tr>
                                            <td><?php echo $value['id']?></td>
                                            <td><?php echo $value['gname']?></td>
                                            <td><?php echo $value['cname']?></td>
                                            <td><?php echo $value['price']?></td>
                                            <td><?php echo $value['stock']?></td>
                                            <td><?php echo $value['sale']?></td>
<!--                                            <td>--><?php //echo $value['is_new']?><!--</td>-->
                                            <td> <a href="./action.php?act=is_new&is_new=<?php echo $value['is_new']?>&id=<?php echo $value['id']?>"><h3><?php echo $value['is_new'] == 0 ?'✘' : '✔';?></h3></a>
                                            </td>

                                            <td> <a href="./action.php?act=is_hot&is_hot=<?php echo $value['is_hot']?>&id=<?php echo $value['id']?>"><h3><?php echo $value['is_hot'] == 0 ?'✘' : '✔';?></h3></a>
                                            </td>

                                            <td> <a href="./action.php?act=state&state=<?php echo $value['state']?>&id=<?php echo $value['id']?>"><h3><?php echo $value['state'] == 0 ?'✘' : '✔';?></h3></a>
                                            </td>
                                            <td><?php echo $value['zan']?></td>



                                        </tr>
                                        <tr>
                                            <th colspan="1"><i class="icon_calendar"></i> 商品图片</th>
                                            <th colspan="6"><i class="icon_calendar"></i> 商品信息</th>
                                            <th colspan="1"><i class="icon_calendar"></i> 创建时间</th>
                                            <th colspan="1"><i class="icon_calendar"></i> 修改时间</th>
                                            <th colspan="2"><i class="icon_cogs"></i> Action</th>
                                        </tr>
                                        <tr>
                                            <td colspan="1">
                                                <img src="<?php echo get_img_url(ADMIN_ROOT_URL . "../upload",$value['iname'],250)?>"
                                                     alt="商品图片">
                                            </td>
                                            <td colspan="6" style="width: 650px"><?php echo $value['msg']?></td>
                                            <td colspan="1"><?php echo $value['create_time']?></td>
                                            <td colspan="1"><?php echo $value['update_time']?></td>
                                            <td colspan="2">
                                                <div class="btn-group">
                                                    <a class="btn btn-success" href="./edit.php?id=<?php echo $value['id']?>">商品信息编辑</a>
                                                    <br><br><br><br>
                                                    <a class="btn btn-danger" href="./img.php?id=<?php echo $value['id'];?>">商品图片编辑</a>

                                                </div>
                                            </td>
                                        </tr>

                                </tbody>
                            </table>
                        <?php endforeach;?>

                        <!--分页开始-->
                        <?php require ADMIN_ROOT_PATH . "../com/page.php"?>
                        <!--分页结束-->
                    </section>
                </div>
            </div>
            <!-- page end-->
        </section>
    </section>
    <!--main content end-->

</section>
<!-- container section start -->

<!-- javascripts -->
<script src="../../public/admin/js/jquery.js"></script>
<script src="../../public/admin/js/jquery-ui-1.10.4.min.js"></script>
<script src="../../public/admin/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../public/admin/js/jquery-ui-1.9.2.custom.min.js"></script>
<!-- bootstrap -->
<script src="../../public/admin/js/bootstrap.min.js"></script>
<!-- nice scroll -->
<script src="../../public/admin/js/jquery.scrollTo.min.js"></script>
<script src="../../public/admin/js/jquery.nicescroll.js" type="text/javascript"></script>
<!-- charts scripts -->
<script src="../../public/admin/assets/jquery-knob/js/jquery.knob.js"></script>
<script src="../../public/admin/js/jquery.sparkline.js" type="text/javascript"></script>
<script src="../../public/admin/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.js"></script>
<script src="../../public/admin/js/owl.carousel.js" ></script>
<!-- jQuery full calendar -->
<<script src="../../public/admin/js/fullcalendar.min.js"></script> <!-- Full Google Calendar - Calendar -->
<script src="../../public/admin/assets/fullcalendar/fullcalendar/fullcalendar.js"></script>
<!--script for this page only-->
<script src="../../public/admin/js/calendar-custom.js"></script>
<script src="../../public/admin/js/jquery.rateit.min.js"></script>
<!-- custom select -->
<script src="../../public/admin/js/jquery.customSelect.min.js" ></script>
<script src="../../public/admin/assets/chart-master/Chart.js"></script>

<!--custome script for all page-->
<script src="../../public/admin/js/scripts.js"></script>
<!-- custom script for this page-->
<script src="../../public/admin/js/sparkline-chart.js"></script>
<script src="../../public/admin/js/easy-pie-chart.js"></script>
<script src="../../public/admin/js/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../../public/admin/js/jquery-jvectormap-world-mill-en.js"></script>
<script src="../../public/admin/js/xcharts.min.js"></script>
<script src="../../public/admin/js/jquery.autosize.min.js"></script>
<script src="../../public/admin/js/jquery.placeholder.min.js"></script>
<script src="../../public/admin/js/gdp-data.js"></script>
<script src="../../public/admin/js/morris.min.js"></script>
<script src="../../public/admin/js/sparklines.js"></script>
<script src="../../public/admin/js/charts.js"></script>
<script src="../../public/admin/js/jquery.slimscroll.min.js"></script>
<script>

    //knob
    $(function() {
        $(".knob").knob({
            'draw' : function () {
                $(this.i).val(this.cv + '%')
            }
        })
    });

    //carousel
    $(document).ready(function() {
        $("#owl-slider").owlCarousel({
            navigation : true,
            slideSpeed : 300,
            paginationSpeed : 400,
            singleItem : true

        });
    });

    //custom select box

    $(function(){
        $('select.styled').customSelect();
    });

    /* ---------- Map ---------- */
    $(function(){
        $('#map').vectorMap({
            map: 'world_mill_en',
            series: {
                regions: [{
                    values: gdpData,
                    scale: ['#000', '#000'],
                    normalizeFunction: 'polynomial'
                }]
            },
            backgroundColor: '#eef3f7',
            onLabelShow: function(e, el, code){
                el.html(el.html()+' (GDP - '+gdpData[code]+')');
            }
        });
    });



</script>

</body>
</html>